{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第1章  数据分析软件简介"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第2章  数据的收集与整理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第3章 Python 编程基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 网上有大量的Python编程基础知识介绍，如 http://www.runoob.com/Python/Python-dictionary.html \n",
    "\n",
    "> 由于本书重点介绍Python的数据分析，所以对Python编程的基础知识将不展开讨论。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.1 Python 编程运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.1.1 基本运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.1.2 控制语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.1.2.1 循环语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in [1,2,3,4]: #range(1,n)表示1到n-1的列表\n",
    "    print(i) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in range(1,5)]  #循环的简洁写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前水果 : banana\n",
      "当前水果 : apple\n",
      "当前水果 : mango\n"
     ]
    }
   ],
   "source": [
    "#fruits = ['banana', 'apple', 'mango'] \n",
    "for fruit in ['banana','apple','mango']:  #fruits: \n",
    "    print('当前水果 :', fruit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.1.2.2 条件语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a的数值小于 100\n"
     ]
    }
   ],
   "source": [
    "a = -100\n",
    "if a < 100:\n",
    "    print(\"a的数值小于 100\") \n",
    "else: \n",
    "    print(\"a的数值大于 100\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-100"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a if a<0 else a   #ifelse的简洁语法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.1.3 函数定义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.1.3.1 内置函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.1.3.2 自定义函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "def 函数名(参数1, 参数2, …)：     \n",
    "      函数体\n",
    "    return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def xbar(x): \n",
    "    n=len(x) \n",
    "    S=sum([i for i in x])    \n",
    "    xbar=S/n              #sum(x)/n\n",
    "    return(xbar) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 6, 4, 9, 7, 5, 8, 2]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=[1,3,6,4,9,7,5,8,2]; X "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xbar(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.mean(X)         #Python已内建这些函数命令，可直接使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.1.4 面向对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def SS1(x):                       #计算离均差平方和函数\n",
    "    n=len(x) \n",
    "    S1=sum([i for i in x])        #计算列表的和\n",
    "    S2x=sum([i*i for i in x])     #计算列表的平方和\n",
    "    Sx2=sum([i for i in x])**2    #计算列表和的平方\n",
    "    SS=S2x-Sx2/n     \n",
    "    return(SS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=[1,3,6,4,9,7,5,8,2]\n",
    "SS1(X) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def SS2(x):                       #返回多个值函数\n",
    "    n=len(x) \n",
    "    S1=sum([i for i in x])        #计算列表的和\n",
    "    xbar=S1/n\n",
    "    S2x=sum([i*i for i in x])     #计算列表的平方和\n",
    "    Sx2=sum([i for i in x])**2    #计算列表和的平方\n",
    "    SS=S2x-Sx2/n     \n",
    "    return[n,S1,xbar,S2x,Sx2,SS]  \n",
    "    #返回例数、均值、平方和、和的平方、离均差平方和的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 45, 5.0, 285, 2025, 60.0]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SS2(X)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SS2(X)[0] #取第 1 个对象 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SS2(X)[1] #取第 2 个对象 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SS2(X)[2] #取第 3 个对象 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "285"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SS2(X)[3] #取第 4 个对象 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2025"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SS2(X)[4] #取第 5 个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60.0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SS2(X)[5] #取第 6 个对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#可以使用 type 函数来查看数据或对象的类型\n",
    "type(SS2(X)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(SS2(X)[3]) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.2 数值分析库 numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell as IS\n",
    "IS.ast_node_interactivity = \"all\"            #多行命令一次输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 　3.2.1 一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np         #加载数组包\n",
    "np.array([1,2,3,4,5])      #一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  2.,  3., nan,  5.])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3,np.nan,5]) #包含缺失值的数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(9)               #数组序列 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. ,  5.5,  6. ,\n",
       "        6.5,  7. ,  7.5,  8. ,  8.5,  9. ,  9.5, 10. , 10.5, 11. , 11.5,\n",
       "       12. , 12.5, 13. , 13.5, 14. , 14.5, 15. , 15.5, 16. , 16.5, 17. ,\n",
       "       17.5, 18. , 18.5])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,19,0.5)         #等差数列 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1. ,  5.5, 10. , 14.5, 19. ])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,19,5)         #等距数列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.2.2 二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4],\n",
       "       [5, 7]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B=np.array([[1,2],[3,4],[5,7]]);B   #二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [3, 4, 5],\n",
       "       [5, 6, 6]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([[1,2,3],[3,4,5],[5,6,6]])   #二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2],\n",
       "       [3, 4, 5],\n",
       "       [6, 7, 8]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A=np.arange(9).reshape((3,3));A #形成 3×3 矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.2.3 数组的操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape            #数组的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape[0]        #数组的行数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.shape[1]        #数组的列数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 2)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 4, 8])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag(A)         #对角阵    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z=np.zeros((3,3));Z    #零矩阵 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1.],\n",
       "       [1., 1., 1.],\n",
       "       [1., 1., 1.]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "J=np.ones((3,3));J     #1矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I=np.eye(3);I          #单位阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2., 1., 1.],\n",
       "       [1., 2., 1.],\n",
       "       [1., 1., 2.]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Z+J+I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I*J"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.3 数据分析库 pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.3.1 序列：Series "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（1）创建序列（向量、一维数组）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（2）生成序列 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd   #加载数据分析包\n",
    "#pd.Series()           #生成空序列 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（3）根据列表构建序列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 3, 6, 4, 9]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[67, 66, 83, 68, 70]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['女', '男', '男', '女', '男']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=[1,3,6,4,9];X\n",
    "weight=[67,66,83,68,70]; weight\n",
    "sex=['女','男','男','女','男']; sex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    3\n",
       "2    6\n",
       "3    4\n",
       "4    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S1=pd.Series(X);S1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    67\n",
       "1    66\n",
       "2    83\n",
       "3    68\n",
       "4    70\n",
       "dtype: int64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S2=pd.Series(weight);S2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    女\n",
       "1    男\n",
       "2    男\n",
       "3    女\n",
       "4    男\n",
       "dtype: object"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S3=pd.Series(sex);S3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（4）序列合并 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    67\n",
       "1    66\n",
       "2    83\n",
       "3    68\n",
       "4    70\n",
       "0     女\n",
       "1     男\n",
       "2     男\n",
       "3     女\n",
       "4     男\n",
       "dtype: object"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([S2,S3],axis=0)   #按行合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1\n",
       "0  1  女\n",
       "1  3  男\n",
       "2  6  男\n",
       "3  4  女\n",
       "4  9  男"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([S1,S3],axis=1)  #按列合并"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（5）序列切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S1[2] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    男\n",
       "2    男\n",
       "3    女\n",
       "dtype: object"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "S3[1:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.3.2 数据框：DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（1）生成数据框 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: []\n",
       "Index: []"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame()      #生成空数据框"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（2）根据列表创建数据框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0\n",
       "0  1\n",
       "1  3\n",
       "2  6\n",
       "3  4\n",
       "4  9"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(X) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   weight\n",
       "A      67\n",
       "B      66\n",
       "C      83\n",
       "D      68\n",
       "E      70"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(weight,columns=['weight'], index=['A','B','C','D','E'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（3）根据字典创建数据框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S1</th>\n",
       "      <th>S2</th>\n",
       "      <th>S3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>67</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>66</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>6</td>\n",
       "      <td>83</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>68</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9</td>\n",
       "      <td>70</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   S1  S2 S3\n",
       "0   1  67  女\n",
       "1   3  66  男\n",
       "2   6  83  男\n",
       "3   4  68  女\n",
       "4   9  70  男"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1=pd.DataFrame({'S1':S1,'S2':S2,'S3':S3}); df1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sex</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>女</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>男</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>男</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>女</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>男</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  sex  weight\n",
       "1   女      67\n",
       "3   男      66\n",
       "6   男      83\n",
       "4   女      68\n",
       "9   男      70"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2=pd.DataFrame({'sex':sex,'weight':weight},index=X);df2 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（4）增加数据框列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sex</th>\n",
       "      <th>weight</th>\n",
       "      <th>weight2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>女</td>\n",
       "      <td>67</td>\n",
       "      <td>4489</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>男</td>\n",
       "      <td>66</td>\n",
       "      <td>4356</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>男</td>\n",
       "      <td>83</td>\n",
       "      <td>6889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>女</td>\n",
       "      <td>68</td>\n",
       "      <td>4624</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>男</td>\n",
       "      <td>70</td>\n",
       "      <td>4900</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  sex  weight  weight2\n",
       "1   女      67     4489\n",
       "3   男      66     4356\n",
       "6   男      83     6889\n",
       "4   女      68     4624\n",
       "9   男      70     4900"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2['weight2']=df2['weight']**2; df2 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（5）删除数据框列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sex</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>女</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>男</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>男</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>女</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>男</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  sex  weight\n",
       "1   女      67\n",
       "3   男      66\n",
       "6   男      83\n",
       "4   女      68\n",
       "9   男      70"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del df2['weight2']; df2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（6）缺失值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S2</th>\n",
       "      <th>S3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>66.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68.0</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>70.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     S2   S3\n",
       "1  66.0    男\n",
       "3  68.0    女\n",
       "6   NaN  NaN\n",
       "4  70.0    男\n",
       "9   NaN  NaN"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3=pd.DataFrame({'S2':S2,'S3':S3},index=S1);df3 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S2</th>\n",
       "      <th>S3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      S2     S3\n",
       "1  False  False\n",
       "3  False  False\n",
       "6   True   True\n",
       "4  False  False\n",
       "9   True   True"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.isnull()        #是缺失值则返回 True，否则返回 False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "S2    2\n",
       "S3    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.isnull().sum()  #返回每列包含的缺失值的个数 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S2</th>\n",
       "      <th>S3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>66.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68.0</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>70.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     S2 S3\n",
       "1  66.0  男\n",
       "3  68.0  女\n",
       "4  70.0  男"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.dropna()       #直接删除含有缺失值的行，多变量谨慎使用 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（7）数据框排序 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S2</th>\n",
       "      <th>S3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>66.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68.0</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>70.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     S2   S3\n",
       "1  66.0    男\n",
       "3  68.0    女\n",
       "6   NaN  NaN\n",
       "4  70.0    男\n",
       "9   NaN  NaN"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S2</th>\n",
       "      <th>S3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>66.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68.0</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>70.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     S2   S3\n",
       "1  66.0    男\n",
       "3  68.0    女\n",
       "4  70.0    男\n",
       "6   NaN  NaN\n",
       "9   NaN  NaN"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3\n",
    "df3.sort_index()         #按index排序 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>S2</th>\n",
       "      <th>S3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>68.0</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>66.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>70.0</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     S2   S3\n",
       "3  68.0    女\n",
       "1  66.0    男\n",
       "4  70.0    男\n",
       "6   NaN  NaN\n",
       "9   NaN  NaN"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3.sort_values(by='S3') #按S3列值排序 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.3.3 数据框的读写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.3.3.1 pandas 读取数据集 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "pd.set_option('display.max_rows', 10)\n",
    "\n",
    "#BSdata=pd.read_clipboard(); #从剪切板上复制数据 \n",
    "#BSdata   #BSdata.head() 见下节"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1510248008</td>\n",
       "      <td>女</td>\n",
       "      <td>167</td>\n",
       "      <td>71</td>\n",
       "      <td>46.0</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1510229019</td>\n",
       "      <td>男</td>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "      <td>10.4</td>\n",
       "      <td>有必要</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>Matlab</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1512108019</td>\n",
       "      <td>女</td>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "      <td>21.0</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1512332010</td>\n",
       "      <td>男</td>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "      <td>4.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1512331015</td>\n",
       "      <td>男</td>\n",
       "      <td>154</td>\n",
       "      <td>55</td>\n",
       "      <td>25.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>1538319004</td>\n",
       "      <td>男</td>\n",
       "      <td>175</td>\n",
       "      <td>68</td>\n",
       "      <td>44.4</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>1538254010</td>\n",
       "      <td>女</td>\n",
       "      <td>166</td>\n",
       "      <td>65</td>\n",
       "      <td>5.3</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>1540294017</td>\n",
       "      <td>女</td>\n",
       "      <td>159</td>\n",
       "      <td>58</td>\n",
       "      <td>71.4</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>1540365026</td>\n",
       "      <td>女</td>\n",
       "      <td>169</td>\n",
       "      <td>73</td>\n",
       "      <td>5.5</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>1540388036</td>\n",
       "      <td>女</td>\n",
       "      <td>165</td>\n",
       "      <td>67</td>\n",
       "      <td>56.8</td>\n",
       "      <td>不必要</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>52 rows × 8 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            学号 性别   身高  体重    支出   开设    课程      软件\n",
       "0   1510248008  女  167  71  46.0  不清楚  都未学过      No\n",
       "1   1510229019  男  171  68  10.4  有必要  概率统计  Matlab\n",
       "2   1512108019  女  175  73  21.0  有必要  统计方法    SPSS\n",
       "3   1512332010  男  169  74   4.9  有必要  编程技术   Excel\n",
       "4   1512331015  男  154  55  25.9  有必要  都学习过  Python\n",
       "..         ... ..  ...  ..   ...  ...   ...     ...\n",
       "47  1538319004  男  175  68  44.4  不清楚  统计方法     SAS\n",
       "48  1538254010  女  166  65   5.3  不清楚  编程技术  Python\n",
       "49  1540294017  女  159  58  71.4  不清楚  都学习过    SPSS\n",
       "50  1540365026  女  169  73   5.5  有必要  统计方法   Excel\n",
       "51  1540388036  女  165  67  56.8  不必要  概率统计     SAS\n",
       "\n",
       "[52 rows x 8 columns]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata=pd.read_csv(\"DaPy_BS.csv\",encoding='utf-8')  #GBK \n",
    "BSdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "BSdata=pd.read_excel('DaPy_data.xlsx','BSdata');# BSdata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.3.3.2 pandas 数据集的保存 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "BSdata.to_csv('BSdata1.csv')    #将数据框BSdata保存到BSdata.csv文档中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "#将数据框 BSdata 保存到BSdata1.xlsx 文档中\n",
    "BSdata.to_excel('BSdata.xlsx',index=False)  #index=False表示不保存行标签"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 　3.3.4 数据框的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- （1）基本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 52 entries, 0 to 51\n",
      "Data columns (total 8 columns):\n",
      " #   Column  Non-Null Count  Dtype  \n",
      "---  ------  --------------  -----  \n",
      " 0   学号      52 non-null     int64  \n",
      " 1   性别      52 non-null     object \n",
      " 2   身高      52 non-null     int64  \n",
      " 3   体重      52 non-null     int64  \n",
      " 4   支出      52 non-null     float64\n",
      " 5   开设      52 non-null     object \n",
      " 6   课程      52 non-null     object \n",
      " 7   软件      52 non-null     object \n",
      "dtypes: float64(1), int64(3), object(4)\n",
      "memory usage: 3.4+ KB\n"
     ]
    }
   ],
   "source": [
    "BSdata.info()     #数据框信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1510248008</td>\n",
       "      <td>女</td>\n",
       "      <td>167</td>\n",
       "      <td>71</td>\n",
       "      <td>46.0</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1510229019</td>\n",
       "      <td>男</td>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "      <td>10.4</td>\n",
       "      <td>有必要</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>Matlab</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1512108019</td>\n",
       "      <td>女</td>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "      <td>21.0</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1512332010</td>\n",
       "      <td>男</td>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "      <td>4.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1512331015</td>\n",
       "      <td>男</td>\n",
       "      <td>154</td>\n",
       "      <td>55</td>\n",
       "      <td>25.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1516248014</td>\n",
       "      <td>男</td>\n",
       "      <td>183</td>\n",
       "      <td>76</td>\n",
       "      <td>85.6</td>\n",
       "      <td>不必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1516352030</td>\n",
       "      <td>女</td>\n",
       "      <td>169</td>\n",
       "      <td>71</td>\n",
       "      <td>9.1</td>\n",
       "      <td>有必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1516171019</td>\n",
       "      <td>女</td>\n",
       "      <td>166</td>\n",
       "      <td>66</td>\n",
       "      <td>2.5</td>\n",
       "      <td>不必要</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1516391008</td>\n",
       "      <td>女</td>\n",
       "      <td>165</td>\n",
       "      <td>69</td>\n",
       "      <td>35.6</td>\n",
       "      <td>不必要</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1520395019</td>\n",
       "      <td>男</td>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "      <td>22.8</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>R</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           学号 性别   身高  体重    支出   开设    课程      软件\n",
       "0  1510248008  女  167  71  46.0  不清楚  都未学过      No\n",
       "1  1510229019  男  171  68  10.4  有必要  概率统计  Matlab\n",
       "2  1512108019  女  175  73  21.0  有必要  统计方法    SPSS\n",
       "3  1512332010  男  169  74   4.9  有必要  编程技术   Excel\n",
       "4  1512331015  男  154  55  25.9  有必要  都学习过  Python\n",
       "5  1516248014  男  183  76  85.6  不必要  编程技术   Excel\n",
       "6  1516352030  女  169  71   9.1  有必要  编程技术   Excel\n",
       "7  1516171019  女  166  66   2.5  不必要  都未学过   Excel\n",
       "8  1516391008  女  165  69  35.6  不必要  都未学过   Excel\n",
       "9  1520395019  男  173  63  22.8  有必要  统计方法       R"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.head(10)     #显示前5行 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>1537288004</td>\n",
       "      <td>女</td>\n",
       "      <td>173</td>\n",
       "      <td>70</td>\n",
       "      <td>19.1</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>1537359035</td>\n",
       "      <td>女</td>\n",
       "      <td>174</td>\n",
       "      <td>71</td>\n",
       "      <td>17.6</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>1438391022</td>\n",
       "      <td>女</td>\n",
       "      <td>164</td>\n",
       "      <td>62</td>\n",
       "      <td>10.3</td>\n",
       "      <td>有必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>1538399025</td>\n",
       "      <td>男</td>\n",
       "      <td>169</td>\n",
       "      <td>65</td>\n",
       "      <td>9.5</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>1438120022</td>\n",
       "      <td>男</td>\n",
       "      <td>166</td>\n",
       "      <td>70</td>\n",
       "      <td>35.6</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>R</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>1538319004</td>\n",
       "      <td>男</td>\n",
       "      <td>175</td>\n",
       "      <td>68</td>\n",
       "      <td>44.4</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>1538254010</td>\n",
       "      <td>女</td>\n",
       "      <td>166</td>\n",
       "      <td>65</td>\n",
       "      <td>5.3</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>1540294017</td>\n",
       "      <td>女</td>\n",
       "      <td>159</td>\n",
       "      <td>58</td>\n",
       "      <td>71.4</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>1540365026</td>\n",
       "      <td>女</td>\n",
       "      <td>169</td>\n",
       "      <td>73</td>\n",
       "      <td>5.5</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>1540388036</td>\n",
       "      <td>女</td>\n",
       "      <td>165</td>\n",
       "      <td>67</td>\n",
       "      <td>56.8</td>\n",
       "      <td>不必要</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            学号 性别   身高  体重    支出   开设    课程      软件\n",
       "42  1537288004  女  173  70  19.1  不清楚  编程技术  Python\n",
       "43  1537359035  女  174  71  17.6  不清楚  概率统计      No\n",
       "44  1438391022  女  164  62  10.3  有必要  编程技术  Python\n",
       "45  1538399025  男  169  65   9.5  有必要  统计方法     SAS\n",
       "46  1438120022  男  166  70  35.6  有必要  统计方法       R\n",
       "47  1538319004  男  175  68  44.4  不清楚  统计方法     SAS\n",
       "48  1538254010  女  166  65   5.3  不清楚  编程技术  Python\n",
       "49  1540294017  女  159  58  71.4  不清楚  都学习过    SPSS\n",
       "50  1540365026  女  169  73   5.5  有必要  统计方法   Excel\n",
       "51  1540388036  女  165  67  56.8  不必要  概率统计     SAS"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.tail(10)     #显示后5行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- （2）数据框列名（变量名） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['学号', '性别', '身高', '体重', '支出', '开设', '课程', '软件'], dtype='object')"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.columns    #查看列名称 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- （3）数据框行名（样品名） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=52, step=1)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.index      #数据框行名 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- （4）数据框维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(52, 8)"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.shape      #显示数据框的行数和列数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "52"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.shape[0]   #数据框行数 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.shape[1]   #数据框列数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- （5）数据框值（数组） "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1510248008, '女', 167, 71, 46.0, '不清楚', '都未学过', 'No'],\n",
       "       [1510229019, '男', 171, 68, 10.4, '有必要', '概率统计', 'Matlab'],\n",
       "       [1512108019, '女', 175, 73, 21.0, '有必要', '统计方法', 'SPSS'],\n",
       "       [1512332010, '男', 169, 74, 4.9, '有必要', '编程技术', 'Excel'],\n",
       "       [1512331015, '男', 154, 55, 25.9, '有必要', '都学习过', 'Python']],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.values[:5] #数据框值数组 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1510248008</td>\n",
       "      <td>女</td>\n",
       "      <td>167</td>\n",
       "      <td>71</td>\n",
       "      <td>46.0</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1510229019</td>\n",
       "      <td>男</td>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "      <td>10.4</td>\n",
       "      <td>有必要</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>Matlab</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1512108019</td>\n",
       "      <td>女</td>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "      <td>21.0</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1512332010</td>\n",
       "      <td>男</td>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "      <td>4.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1512331015</td>\n",
       "      <td>男</td>\n",
       "      <td>154</td>\n",
       "      <td>55</td>\n",
       "      <td>25.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           学号 性别   身高  体重    支出   开设    课程      软件\n",
       "0  1510248008  女  167  71  46.0  不清楚  都未学过      No\n",
       "1  1510229019  男  171  68  10.4  有必要  概率统计  Matlab\n",
       "2  1512108019  女  175  73  21.0  有必要  统计方法    SPSS\n",
       "3  1512332010  男  169  74   4.9  有必要  编程技术   Excel\n",
       "4  1512331015  男  154  55  25.9  有必要  都学习过  Python"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>1538319004</td>\n",
       "      <td>男</td>\n",
       "      <td>175</td>\n",
       "      <td>68</td>\n",
       "      <td>44.4</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>1538254010</td>\n",
       "      <td>女</td>\n",
       "      <td>166</td>\n",
       "      <td>65</td>\n",
       "      <td>5.3</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>1540294017</td>\n",
       "      <td>女</td>\n",
       "      <td>159</td>\n",
       "      <td>58</td>\n",
       "      <td>71.4</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>1540365026</td>\n",
       "      <td>女</td>\n",
       "      <td>169</td>\n",
       "      <td>73</td>\n",
       "      <td>5.5</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>1540388036</td>\n",
       "      <td>女</td>\n",
       "      <td>165</td>\n",
       "      <td>67</td>\n",
       "      <td>56.8</td>\n",
       "      <td>不必要</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            学号 性别   身高  体重    支出   开设    课程      软件\n",
       "47  1538319004  男  175  68  44.4  不清楚  统计方法     SAS\n",
       "48  1538254010  女  166  65   5.3  不清楚  编程技术  Python\n",
       "49  1540294017  女  159  58  71.4  不清楚  都学习过    SPSS\n",
       "50  1540365026  女  169  73   5.5  有必要  统计方法   Excel\n",
       "51  1540388036  女  165  67  56.8  不必要  概率统计     SAS"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.head()\n",
    "BSdata.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.3.4.2 选取变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（1）[' ']或“.”法或："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    167\n",
       "1    171\n",
       "2    175\n",
       "3    169\n",
       "4    154\n",
       "Name: 身高, dtype: int64"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata['身高'].head()          #取一列数据，一列时也可用BSdata.身高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    167\n",
       "1    171\n",
       "2    175\n",
       "3    169\n",
       "4    154\n",
       "Name: 身高, dtype: int64"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.身高.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>女</td>\n",
       "      <td>167</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>男</td>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>女</td>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>男</td>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>男</td>\n",
       "      <td>154</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  性别   身高  体重\n",
       "0  女  167  71\n",
       "1  男  171  68\n",
       "2  女  175  73\n",
       "3  男  169  74\n",
       "4  男  154  55"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata[['性别','身高','体重']].head() #取两列数据 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（2）下标法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    身高  体重\n",
       "1  171  68\n",
       "2  175  73"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.iloc[1:3,2:4]      #取第1列 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>154</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>183</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>169</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>166</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>165</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>173</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    身高  体重\n",
       "1  171  68\n",
       "2  175  73\n",
       "3  169  74\n",
       "4  154  55\n",
       "5  183  76\n",
       "6  169  71\n",
       "7  166  66\n",
       "8  165  69\n",
       "9  173  63"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.iloc[1:10,2:4]   #取3、4列 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.3.4.3 提取样品 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "学号    1512332010\n",
       "性别             男\n",
       "身高           169\n",
       "体重            74\n",
       "支出           4.9\n",
       "开设           有必要\n",
       "课程          编程技术\n",
       "软件         Excel\n",
       "Name: 3, dtype: object"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.loc[3]        #取第4行 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1512332010</td>\n",
       "      <td>男</td>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "      <td>4.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1512331015</td>\n",
       "      <td>男</td>\n",
       "      <td>154</td>\n",
       "      <td>55</td>\n",
       "      <td>25.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1516248014</td>\n",
       "      <td>男</td>\n",
       "      <td>183</td>\n",
       "      <td>76</td>\n",
       "      <td>85.6</td>\n",
       "      <td>不必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           学号 性别   身高  体重    支出   开设    课程      软件\n",
       "3  1512332010  男  169  74   4.9  有必要  编程技术   Excel\n",
       "4  1512331015  男  154  55  25.9  有必要  都学习过  Python\n",
       "5  1516248014  男  183  76  85.6  不必要  编程技术   Excel"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.loc[3:5]      #取3至5行 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.3.4.4  选取观测与变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    身高  体重\n",
       "1  171  68\n",
       "2  175  73\n",
       "3  169  74"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.loc[1:3,['身高','体重']] # 按照标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>男</td>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "      <td>10.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>女</td>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "      <td>21.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  性别   身高  体重    支出\n",
       "1  男  171  68  10.4\n",
       "2  女  175  73  21.0"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.iloc[1:3,1:5]  #0至2行和1至5列数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.3.4.5 条件选取 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1516248014</td>\n",
       "      <td>男</td>\n",
       "      <td>183</td>\n",
       "      <td>76</td>\n",
       "      <td>85.6</td>\n",
       "      <td>不必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1520100029</td>\n",
       "      <td>男</td>\n",
       "      <td>184</td>\n",
       "      <td>82</td>\n",
       "      <td>10.3</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1525352033</td>\n",
       "      <td>男</td>\n",
       "      <td>185</td>\n",
       "      <td>83</td>\n",
       "      <td>5.1</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>1530243029</td>\n",
       "      <td>男</td>\n",
       "      <td>186</td>\n",
       "      <td>87</td>\n",
       "      <td>9.5</td>\n",
       "      <td>不必要</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            学号 性别   身高  体重    支出   开设    课程     软件\n",
       "5   1516248014  男  183  76  85.6  不必要  编程技术  Excel\n",
       "10  1520100029  男  184  82  10.3  有必要  都学习过    SAS\n",
       "21  1525352033  男  185  83   5.1  有必要  都学习过   SPSS\n",
       "32  1530243029  男  186  87   9.5  不必要  都未学过     No"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata[BSdata['身高']>180] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1520100029</td>\n",
       "      <td>男</td>\n",
       "      <td>184</td>\n",
       "      <td>82</td>\n",
       "      <td>10.3</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1525352033</td>\n",
       "      <td>男</td>\n",
       "      <td>185</td>\n",
       "      <td>83</td>\n",
       "      <td>5.1</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>1530243029</td>\n",
       "      <td>男</td>\n",
       "      <td>186</td>\n",
       "      <td>87</td>\n",
       "      <td>9.5</td>\n",
       "      <td>不必要</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            学号 性别   身高  体重    支出   开设    课程    软件\n",
       "10  1520100029  男  184  82  10.3  有必要  都学习过   SAS\n",
       "21  1525352033  男  185  83   5.1  有必要  都学习过  SPSS\n",
       "32  1530243029  男  186  87   9.5  不必要  都未学过    No"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata[(BSdata['身高']>180) & (BSdata['体重']>80)] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1516248014</td>\n",
       "      <td>男</td>\n",
       "      <td>183</td>\n",
       "      <td>76</td>\n",
       "      <td>85.6</td>\n",
       "      <td>不必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1520100029</td>\n",
       "      <td>男</td>\n",
       "      <td>184</td>\n",
       "      <td>82</td>\n",
       "      <td>10.3</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SAS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1523376027</td>\n",
       "      <td>男</td>\n",
       "      <td>180</td>\n",
       "      <td>81</td>\n",
       "      <td>64.1</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>Excel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1525352033</td>\n",
       "      <td>男</td>\n",
       "      <td>185</td>\n",
       "      <td>83</td>\n",
       "      <td>5.1</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>SPSS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>1530243029</td>\n",
       "      <td>男</td>\n",
       "      <td>186</td>\n",
       "      <td>87</td>\n",
       "      <td>9.5</td>\n",
       "      <td>不必要</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>No</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            学号 性别   身高  体重    支出   开设    课程     软件\n",
       "5   1516248014  男  183  76  85.6  不必要  编程技术  Excel\n",
       "10  1520100029  男  184  82  10.3  有必要  都学习过    SAS\n",
       "15  1523376027  男  180  81  64.1  有必要  统计方法  Excel\n",
       "21  1525352033  男  185  83   5.1  有必要  都学习过   SPSS\n",
       "32  1530243029  男  186  87   9.5  不必要  都未学过     No"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata[(BSdata['身高']>180) | (BSdata['体重']>80)] "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 3.3.4.6 数据框的运算 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（1）生成新的数据框 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>学号</th>\n",
       "      <th>性别</th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "      <th>支出</th>\n",
       "      <th>开设</th>\n",
       "      <th>课程</th>\n",
       "      <th>软件</th>\n",
       "      <th>体重指数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1510248008</td>\n",
       "      <td>女</td>\n",
       "      <td>167</td>\n",
       "      <td>71</td>\n",
       "      <td>46.0</td>\n",
       "      <td>不清楚</td>\n",
       "      <td>都未学过</td>\n",
       "      <td>No</td>\n",
       "      <td>25.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1510229019</td>\n",
       "      <td>男</td>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "      <td>10.4</td>\n",
       "      <td>有必要</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>Matlab</td>\n",
       "      <td>23.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1512108019</td>\n",
       "      <td>女</td>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "      <td>21.0</td>\n",
       "      <td>有必要</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>SPSS</td>\n",
       "      <td>23.84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1512332010</td>\n",
       "      <td>男</td>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "      <td>4.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>Excel</td>\n",
       "      <td>25.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1512331015</td>\n",
       "      <td>男</td>\n",
       "      <td>154</td>\n",
       "      <td>55</td>\n",
       "      <td>25.9</td>\n",
       "      <td>有必要</td>\n",
       "      <td>都学习过</td>\n",
       "      <td>Python</td>\n",
       "      <td>23.19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           学号 性别   身高  体重    支出   开设    课程      软件   体重指数\n",
       "0  1510248008  女  167  71  46.0  不清楚  都未学过      No  25.46\n",
       "1  1510229019  男  171  68  10.4  有必要  概率统计  Matlab  23.26\n",
       "2  1512108019  女  175  73  21.0  有必要  统计方法    SPSS  23.84\n",
       "3  1512332010  男  169  74   4.9  有必要  编程技术   Excel  25.91\n",
       "4  1512331015  男  154  55  25.9  有必要  都学习过  Python  23.19"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata['体重指数']=BSdata['体重']/(BSdata['身高']/100)**2\n",
    "round(BSdata[:5],2) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（2）数据框的合并 concat() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     167\n",
       "1     171\n",
       "2     175\n",
       "3     169\n",
       "4     154\n",
       "     ... \n",
       "47     68\n",
       "48     65\n",
       "49     58\n",
       "50     73\n",
       "51     67\n",
       "Length: 104, dtype: int64"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([BSdata.身高, BSdata.体重],axis=0) # 按行合并 axis=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>身高</th>\n",
       "      <th>体重</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>167</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>171</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>175</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>169</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>154</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>175</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>166</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>159</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>169</td>\n",
       "      <td>73</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>165</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>52 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     身高  体重\n",
       "0   167  71\n",
       "1   171  68\n",
       "2   175  73\n",
       "3   169  74\n",
       "4   154  55\n",
       "..  ...  ..\n",
       "47  175  68\n",
       "48  166  65\n",
       "49  159  58\n",
       "50  169  73\n",
       "51  165  67\n",
       "\n",
       "[52 rows x 2 columns]"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([BSdata.身高, BSdata.体重],axis=1) #按列合并 axis=1 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "（3）数据框转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>学号</th>\n",
       "      <td>1510248008</td>\n",
       "      <td>1510229019</td>\n",
       "      <td>1512108019</td>\n",
       "      <td>1512332010</td>\n",
       "      <td>1512331015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>性别</th>\n",
       "      <td>女</td>\n",
       "      <td>男</td>\n",
       "      <td>女</td>\n",
       "      <td>男</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>身高</th>\n",
       "      <td>167</td>\n",
       "      <td>171</td>\n",
       "      <td>175</td>\n",
       "      <td>169</td>\n",
       "      <td>154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>体重</th>\n",
       "      <td>71</td>\n",
       "      <td>68</td>\n",
       "      <td>73</td>\n",
       "      <td>74</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>支出</th>\n",
       "      <td>46.0</td>\n",
       "      <td>10.4</td>\n",
       "      <td>21.0</td>\n",
       "      <td>4.9</td>\n",
       "      <td>25.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>开设</th>\n",
       "      <td>不清楚</td>\n",
       "      <td>有必要</td>\n",
       "      <td>有必要</td>\n",
       "      <td>有必要</td>\n",
       "      <td>有必要</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>课程</th>\n",
       "      <td>都未学过</td>\n",
       "      <td>概率统计</td>\n",
       "      <td>统计方法</td>\n",
       "      <td>编程技术</td>\n",
       "      <td>都学习过</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>软件</th>\n",
       "      <td>No</td>\n",
       "      <td>Matlab</td>\n",
       "      <td>SPSS</td>\n",
       "      <td>Excel</td>\n",
       "      <td>Python</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             0           1           2           3           4\n",
       "学号  1510248008  1510229019  1512108019  1512332010  1512331015\n",
       "性别           女           男           女           男           男\n",
       "身高         167         171         175         169         154\n",
       "体重          71          68          73          74          55\n",
       "支出        46.0        10.4        21.0         4.9        25.9\n",
       "开设         不清楚         有必要         有必要         有必要         有必要\n",
       "课程        都未学过        概率统计        统计方法        编程技术        都学习过\n",
       "软件          No      Matlab        SPSS       Excel      Python"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BSdata.iloc[:5,:8].T       #数据框转置 .T"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {
    "height": "422px",
    "width": "329px"
   },
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "",
   "title_sidebar": "",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "283px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "toc-showcode": false,
  "toc-showmarkdowntxt": false,
  "toc-showtags": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
